package com.hyw.encryption.utils;

import java.util.Map;

public final class RSAUtils {

  // public static void main(String[] a) {
  // Entry<String, String> keyPair = generate();
  // String publicKey = keyPair.getKey();
  // String privateKey = keyPair.getValue();
  // System.out.println(publicKey);
  // System.out.println(privateKey);
  //
  // String inputStr =
  // "{\"eventId\":\"fdb64c8fdf0240fb8ae6974c2a2f08a3\",\"giftId\":154,\"giftVersion\":1,\"liveCode\":\"LI029441\",\"quantity\":1,\"source\":\"Test00000722\",\"target\":\"Test00000700\",\"terminal\":\"ANDROID\"}";
  //
  // byte[] encodedbytes = RSACoder.encryptByPublicKey(inputStr.getBytes(), publicKey);
  // System.out.println("===============encrypted=================");
  // String encodedStr = Base64.getEncoder().encodeToString(encodedbytes);
  // System.out.println(encodedStr);
  //
  // String decrypted = new String(
  // RSACoder.decryptByPrivateKey(Base64.getDecoder().decode(encodedStr.getBytes()), privateKey));
  // System.out.println("===============decrypted=================");
  // System.out.println(decrypted);
  // }

  /**
   * 生成一对RSA公钥和私钥, 其中string[0]为公钥, string[1]为私钥
   * 
   * @return
   */
  public static String[] generate() {
    try {
      Map<String, Object> keyMap = RSACoder.initKey();
      String publicKey = RSACoder.getPublicKey(keyMap);
      String privateKey = RSACoder.getPrivateKey(keyMap);
      return new String[] { publicKey, privateKey };
    } catch (Exception ex) {
      throw new IllegalArgumentException(ex);
    }
  }

}
